﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Web.Framework;
using System.Web.Security;
using Entity.Admin;

namespace CMP.Web.Controllers
{
    public partial class HomeController : BaseController
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
            //return RedirectToAction("Index", "Account", new { area = "Manager" });
        }

        public ActionResult GetTime()
        {
            return Content(DateTime.Now.ToString());
        }

        public ActionResult Setup()
        {
            db.Database.CreateIfNotExists();

            var menu = new Menu() { ID = Guid.NewGuid().ToString(), CheckRight = false, Enable = true, MenuText = "系统管理", OrderIndex = 1 };

            var menu0 = new Menu() { ID = Guid.NewGuid().ToString(), CheckRight = false, Enable = true, MenuText = "系统设定", OrderIndex = 1, ParentID = menu.ID };

            var menu1 = new Menu() { ID = Guid.NewGuid().ToString(), CheckRight = false, Enable = true, MenuText = "菜单管理", OrderIndex = 1, Area = "admin", Controller = "menu", Action = "index", ParentID = menu0.ID };
            var menu2 = new Menu() { ID = Guid.NewGuid().ToString(), CheckRight = false, Enable = true, MenuText = "角色管理", OrderIndex = 2, Area = "admin", Controller = "role", Action = "index", ParentID = menu0.ID };
            var menu3 = new Menu() { ID = Guid.NewGuid().ToString(), CheckRight = false, Enable = true, MenuText = "用户管理", OrderIndex = 3, Area = "admin", Controller = "user", Action = "index", ParentID = menu0.ID };

            var menu4 = new Menu() { ID = Guid.NewGuid().ToString(), CheckRight = false, Enable = true, MenuText = "报表设定", OrderIndex = 2, ParentID = menu.ID};
            var menu5 = new Menu() { ID = Guid.NewGuid().ToString(), CheckRight = false, Enable = true, MenuText = "查询条件", OrderIndex = 1, Area = "admin", Controller = "InquiryParameter", Action = "index", ParentID = menu4.ID };
            var menu6 = new Menu() { ID = Guid.NewGuid().ToString(), CheckRight = false, Enable = true, MenuText = "参数规则", OrderIndex = 2, Area = "admin", Controller = "ParameterRule", Action = "index", ParentID = menu4.ID };
            

            db.Menus.Add(menu);
            db.Menus.Add(menu0);
            db.Menus.Add(menu1);
            db.Menus.Add(menu2);
            db.Menus.Add(menu3);
            db.Menus.Add(menu4);
            db.Menus.Add(menu5);
            db.Menus.Add(menu6);

            var role = new Role() { ID = Guid.NewGuid().ToString(), RoleName = "管理员", RoleMemo = "系统初始化的管理员" };
            role.Menus.Add(menu);
            role.Menus.Add(menu0);
            role.Menus.Add(menu1);
            role.Menus.Add(menu2);
            role.Menus.Add(menu3);
            role.Menus.Add(menu4);
            role.Menus.Add(menu5);
            role.Menus.Add(menu6);
            db.Roles.Add(role);


            var user = new User() { ID = Guid.NewGuid().ToString(), CreateDate = DateTime.Now, Comment = "管理员", Email = "songjiang.yang@newtouch.cn", IslockedOut = false, Password = "admin", UserName = "admin" };
            db.Users.Add(user);

            role.Users.Add(user);

            db.SaveChanges();
            return Content("OK");

        }
    }
}
